// CourseListServlet.java

package course;

import java.io.IOException;
import java.sql.*;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class CourseListServlet extends HttpServlet {

	private static org.apache.log4j.Logger log = Logger.getLogger(CourseListServlet.class);
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {

		HttpSession session = req.getSession();
		String sortord = req.getParameter("ord");
    	
		PreparedStatement ps = null;
		Connection con = null;

	    try {
	    	try {
				con = DBcon.getConnection();
	    		log.info("PL-Connected!" );
/*
	    		if (coutype == null || sortord.equals("9")) {
	    			coutype = "%";
	    		}
*/	    		
	    		if (sortord == null || sortord.equals("9")) {
	    			sortord = "1";
	    		}

	    		List<Course> couList =new ArrayList<Course>();
	    		
	    		ps = con.prepareStatement("select cr_id, cr_descr, cr_type, cr_startdate, cr_enddate, cr_hours  from course " +
	    				"  order by ?" );

	    		try{
	    			ps.setString(1, sortord);
	    			
	    			ResultSet rs = ps.executeQuery();
	    			log.info("PL-course is selected" );
	    			
	    			while (rs.next()){
	    				String cr_id = rs.getString(1);
	    				String cr_descr = rs.getString(2);
	    				String cr_type =rs.getString(3);
	    				Date sd = rs.getDate(4);
	    				Date ed = rs.getDate(5);
	    				Integer cr_hours =rs.getInt(6);
	  System.out.print("sd = " + sd );  				
	    				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
	 	  System.out.print("sf = " + sf );  				
	    				
	    				String cr_startdate = sf.format(sd);
	  	  System.out.print("sd = " + sd + "sf = " + sf +" cr_startdate = " + cr_startdate);  				
	    				
	    				String cr_enddate = sf.format(ed);
	    		
	    				Course pro = new Course(cr_id, cr_descr, cr_type, cr_startdate, cr_enddate, cr_hours);
	    				couList.add(pro);
	    			}
	    			con.commit();
	    	    	
	    			req.setAttribute("couList", couList);
	    			req.getRequestDispatcher("courselist.jsp").forward(req,resp );
	    		}finally{
	    			ps.close();
	    		}
	    	}finally {
	    		con.commit();
	    		con.close();
	    	}
	    }catch (SQLException e){
	    	throw new RuntimeException (e);
	    } finally {
	    	try {
	    		ps.close();
	    		con.close();
	    	} catch (SQLException e) {
	    		e.printStackTrace();
	    		log.debug("UI-update fail!!");
	    	}
	    }
	}

	private SimpleDateFormat SimpleDateFormat(String string) {
		// TODO Auto-generated method stub
		return null;
	}
}



